{template 'common/header'}
<ul class="nav nav-tabs">
	<li><a href="{php echo url('activity/coupon/display', array());}">卡券列表</a></li>
	<li class="active"><a href="javascript:;">添加卡券</a></li>
</ul>
<div class="main">
	<form action="" method="post" id="coupon-form" class="form-horizontal" enctype="multipart/form-data">
		<div class="panel panel-default" id="step1">
			<div class="panel-heading">
				{$coupon_title}
			</div>
			<div class="panel-body">
				<div class="pull-left" style="width:320px;background:#F4F5F9;margin-right:20px;border:1px solid #E7E7EB">
					<div class="card-title">{$coupon_title}</div>
					<div class="card_section area" style="position: relative">
						<div class="shop-panel" id="color-purview" style="background: #a9d92d">
							<div class="logo-area">
								<span class="logo pull-left"><img src="{$setting['logourl']}" alt=""/></span>
								<div class="pull-left" style="height:38px;line-height:38px" id="brand_name">商户信息</div>
								<div class="clear"></div>
							</div>
							<div class="msg-area">
								<div class="tick-msg">
									<p><b id="title-purview">{$coupon_title}标题</b></p>
									<p id="sub-title-purview">{$coupon_title}副标题</p>
								</div>
								<p id="sub-time-purview" style="text-align: center">有效期:{php echo date('Y-m-d')} ~ {php echo date('Y-m-d', time() + 30 * 86400)}</p>
							</div>
							<div class="deco"></div>
							<div class="cicon">
								<a href="javascript:;" data-id="form1"><i class="fa fa-pencil"></i></a>
							</div>
						</div>
					</div>
					<div class="card-dispose area">
						<div class="unset" id="destroy_title">
							<p>销券设置</p>
						</div>
						<div class="destroy_type_preview">
							<div class="barcode-area code_preview code_preview_3" style="display: block">
								<div class="barcode"></div>
								<p class="code_num">1513-2290-1878</p>
							</div>
							<div class="qrcode-area code_preview code_preview_2">
								<div class="qrcode"></div>
								<p class="code_num">1513-2290-1878</p>
							</div>
							<div class="sn-area code_preview code_preview_1">1513-2290-1878</div>
							<p style="text-align: center" id="notice-purview">请在付款时出示此券</p>
						</div>
						<div class="cicon">
							<a href="javascript:;" data-id="form2"><i class="fa fa-pencil"></i></a>
						</div>
					</div>
					<div class="shop-detail">
						<ul class="list">
							<li>
								<div class="li-panel area">
									<div class="li-content">{$coupon_title}设置</div>
									<span class="ricon fa fa-angle-right"></span>
									<div class="cicon">
										<a href="javascript:;" data-id="form3"><i class="fa fa-pencil"></i></a>
									</div>
								</div>
							</li>
							<li>
								<div class="li-panel area">
									<div class="li-content">适用门店</div>
									<span class="ricon fa fa-angle-right"></span>
									<div class="cicon">
										<a href="javascript:;" data-id="form4"><i class="fa fa-pencil"></i></a>
									</div>
								</div>
							</li>
						</ul>
					</div>
					<div class="shop-detail">
						<ul class="list">
							<li>
								<div class="li-panel">
									<div class="li-content">立即使用</div>
									<span class="ricon">系统已实现</span>
								</div>
							</li>
							<li>
								<div class="li-panel area" style="border-bottom: none">
									<div class="li-content" id="promotion_url_name">个人中心</div>
									<div class="ricon"><span id="promotion_url_sub_title">点击进入 </span><i class="fa fa-angle-right"></i></div>
									<div class="cicon">
										<a href="javascript:;" data-id="form6"><i class="fa fa-pencil"></i></a>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
				<div class="pull-left alert form form-area" style="width:700px;position:relative;display:block;" id="form1">
					<div class="arrow_in"></div>
					<h4 style="margin-top:20px;">券面信息</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label">是否上架</label>
						<div class="col-sm-9 col-xs-12">
							<label class="radio-inline">
								<input type="radio" value="1" name="is_display" checked/> 上架
							</label>
							<label class="radio-inline">
								<input type="radio" value="0" name="is_display"/> 下架
							</label>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 适用模块</label>
						<div class="col-sm-9 col-xs-12">
							<a href="javascript:;" class="btn btn-default js-modules-modal" data-toggle="modal">选择模块</a>
							<input type="hidden" name="module-select" value="" autocomplete="off" />
							<div class="js-modules-contain clearfix"></div>
							<div class="help-block">确定该券的适用模块，不选择则为全部可用</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 商户LOGO</label>
						<div class="col-sm-9 col-xs-12" id="logo_upload">
							{if COUPON_TYPE == WECHAT_COUPON}
							{php echo tpl_form_field_wechat_image('logo_url', $setting['logourl'], '', array('acid' => $acid, 'mode' => 'file_upload'));}
							{else}
							{php echo tpl_form_field_image('logo_url', $setting['logourl'])}
							{/if}
							<div class="help-block">商户LOGO大小不超过1M。像素为：300*300。仅支持JPG格式</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 商户名称</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="brand_name"/>
							<div class="help-block">商户名字,字数上限为12个汉字。 </div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 卡券颜色</label>
						{php echo tpl_coupon_colors('color', 'Color082');}
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> {$coupon_title}标题</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="title"/>
							<div class="help-block">建议填写{$coupon_title}“减免金额”及自定义内容，描述卡券提供的具体优惠。不超过9个字符</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"> {$coupon_title}副标题(可选)</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="sub_title"/>
							<div class="help-block">不超过18个字符</div>
						</div>
					</div>
					{if $type == COUPON_TYPE_DISCOUNT}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 折扣额度</label>
						<div class="col-sm-9 col-xs-12">
							<div class="input-group">
								<input type="text" class="form-control" name="discount" value="80"/>
								<span class="input-group-addon">折</span>
							</div>
							<div class="help-block">表示打折额度（百分比）。填30就是3折。填写整数</div>
						</div>
					</div>
					{elseif $type == COUPON_TYPE_CASH}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 减免金额</label>
						<div class="col-sm-9 col-xs-12">
							<div class="input-group">
								<span class="input-group-addon">满</span>
								<input type="text" class="form-control" name="least_cost" value=""/>
								<span class="input-group-addon">减</span>
								<input type="text" class="form-control" name="reduce_cost"/>
								<span class="input-group-addon">元</span>
							</div>
						</div>
					</div>
					{elseif $type == COUPON_TYPE_GIFT}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 礼品券详情</label>
						<div class="col-sm-9 col-xs-12">
							<textarea name="gift" id="gift" class="form-control" rows="7"></textarea>
						</div>
					</div>
					{elseif $type == COUPON_TYPE_GROUPON}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 团购券详情</label>
						<div class="col-sm-9 col-xs-12">
							<textarea name="deal_detail" id="deal_detail" class="form-control" rows="7"></textarea>
						</div>
					</div>
					{elseif $type == COUPON_TYPE_GENERAL}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 优惠券详情</label>
						<div class="col-sm-9 col-xs-12">
							<textarea name="default_detail" id="default_detail" class="form-control" rows="7"></textarea>
						</div>
					</div>
					{/if}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label" style="padding-top:15px"> 有效期</label>
						<div class="col-sm-9 col-xs-12">
							<div class="radio">
								<div class="col-sm-3" style="padding-top:6px"><label><input type="radio" value="1" name="time_type" checked/>固定日期 </label></div>
								{php echo tpl_form_field_daterange('time_limit', array('start' => date('Y-m-d'), 'end' => date('Y-m-d', time() + 30*86400)));}
							</div>
							<div class="radio">
								<div class="col-sm-3" style="padding-top:6px"><label><input type="radio" value="2" name="time_type"/>领取后 </label></div>
								<div class="col-sm-3" style="margin-left:-15px">
									<select name="deadline" id="deadline" class="form-control">
										<?php
										for($i=0; $i<=90; $i++) {
											if(!$i) $n = '当'; else $n = $i;
											echo '<option value="'.$i.'">'.$n.'天</option>';
										}
										?>
									</select>
								</div>
								<div class="col-sm-3" style="padding-top:6px">有效,有效期</div>
								<div class="col-sm-3" style="margin-left:-15px">
									<select name="limit" id="limit" class="form-control">
										<?php
										for($i=1; $i<=90; $i++) {
											echo '<option value="'.$i.'">'.$i.'天</option>';
										}
										?>
									</select>
								</div>

							</div>
						</div>
					</div>
				</div>
				<div class="pull-left alert form hidden form-area" style="width:700px;position:relative;" id="form2">
					<div class="arrow_in"></div>
					<h4 style="margin-top:20px;">领券设置</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 库存</label>
						<div class="col-sm-9 col-xs-12">
							<div class="input-group">
								<input type="text" class="form-control" name="quantity" value="300"/>
								<span class="input-group-addon">份</span>
							</div>
							<div class="help-block">库存只能是大于0的数字</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 领券限制</label>
						<div class="col-sm-9 col-xs-12">
							<div class="input-group">
								<input type="text" class="form-control" name="get_limit" value="1"/>
								<span class="input-group-addon">份</span>
							</div>
							<div class="help-block">默认领券限制为1</div>
						</div>
					</div>
					{if COUPON_TYPE == WECHAT_COUPON}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
						<div class="col-sm-9 col-xs-12">
							<div class="checkbox">
								<label><input type="checkbox" name="can_share" value="1" checked /> 用户可以分享领券链接</label>
							</div>
							<div class="checkbox">
								<label><input type="checkbox" name="can_give_friend" value="1" checked /> 用户领券后可转赠其他好友</label>
							</div>
						</div>
					</div>
					<h4 style="margin-top:20px;">销券设置</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label">销券方式</label>
						<div class="col-sm-9 col-xs-12">
							<div class="radio">
								<label><input type="radio"  class="" name="code_type" value="1"/> 仅卡券号</label>
								<div class="help-block">只显示卡券号，验证后可进行销券</div>
							</div>
							<div class="radio">
								<label><input type="radio"  class="" name="code_type" value="2"/> 二维码</label>
								<div class="help-block">包含卡券信息的二维码，扫描后可进行销券</div>
							</div>
							<div class="radio">
								<label><input type="radio"  class="" name="code_type" value="3" checked/> 条形码</label>
								<div class="help-block">包含卡券信息的条形码，扫描后可进行销券</div>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 操作提示</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="notice" id="notice" value="请在付款时出示此券"/>
							<div class="help-block">建议引导用户到店出示卡券，由店员完成核销操作。不超过16个字符</div>
						</div>
					</div>
					{/if}
				</div>
				<div class="pull-left alert form hidden form-area" style="width:700px;position:relative;" id="form3">
					<div class="arrow_in"></div>
					<h4 style="margin-top:20px;">{$coupon_title}详情</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 使用须知</label>
						<div class="col-sm-9 col-xs-12">
							<textarea name="description" id="description" class="form-control" rows="10"></textarea>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 客服电话</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="service_phone"/>
							<div class="help-block">手机或固话</div>
						</div>
					</div>
				</div>
				<div class="pull-left alert form hidden form-area" style="width:700px;position:relative;" id="form4">
					<input type="hidden" name="location-select" value="" autocomplete="off"/>
					<input type="hidden" name="groups" value="" autocomplete="off"/>
					<div class="arrow_in"></div>
					<h4 style="margin-top:20px;">服务信息</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 适用门店</label>
						<div class="col-sm-9 col-xs-12">
							<label class="radio"><a href="javascript:;" data-toggle="modal" class="js-location-store-modal">选择适用门店</a></label>
							<div class="js-location-contain"></div>
							<div class="help-block">默认为适用于全部门店</div>
						</div>
					</div>
					{if COUPON_TYPE == WECHAT_COUPON}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 可用会员组</label>
						<div class="col-sm-9 col-xs-12">
							<label class="radio"><a href="javascript:;" data-toggle="modal" class="js-groups-modal">选择可用会员组</a></label>
							<div class="js-groups-contain"></div>
							<div class="help-block">默认为全部用户组可用</div>
						</div>
					</div>
					{/if}
				</div>
				<div class="pull-left alert form hidden form-area" style="width:700px;position:relative;" id="form6">
					<div class="arrow_in"></div>
					<h4 style="margin-top:20px;">营销入口</h4>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 营销场景的自定义入口名称</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="promotion_url_name" value="个人中心"/>
							<div class="help-block">如：个人中心。</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 跳转链接</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="promotion_url" value="{php echo murl('mc/home', array(), true, true);}"/>
							<div class="help-block">入口跳转链接地址。需要以"http://"或"https://"开头</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger"></span> 营销入口右侧的提示语</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" class="form-control" name="promotion_url_sub_title" value="点击进入"/>
							<div class="help-block">营销入口右侧的提示语.不超过6个汉字</div>
						</div>
					</div>
				</div>
				</div>
		</div>
		<div class="form-group col-sm-12">
			<input name="submit" type="submit" value="提交" class="btn btn-primary col-lg-1">
			<input type="hidden" name="token" value="{$_W['token']}" />
		</div>
	</form>
</div>
<div id="footer-location" class="hide">
	<span name="submit" id="submit" class="pull-right btn btn-primary">保存</span>
</div>
<!--选择门店弹出窗-->
<div class="modal fade" id="location-store-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">选择适用门店</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">
				<table class="table tb" style="min-width:568px;">
					<thead>
					<tr>
						<th style="width: 35px;" class="text-center">选择</th>
						<th style="width: 100px;">门店名称</th>
						<th style="width: 100px;">分店名称</th>
						<th style="width: 100px;">地址</th>
					</tr>
					</thead>
					<tbody>
					{if !empty($location_store)}
						{loop $location_store $row}
							<tr id="loca-{$row['id']}">
								<td class="text-center"><input type="checkbox" id="chk_loca_{$row['id']}" name="locationids[]" data-name="{$row['business_name']}" data-id="{$row['id']}" value="{$row['id']}"></td>
								<td><label for="chk_loca_{$row['id']}" style="font-weight:normal;" class="name">{$row['business_name']}</label></td>
								<td><label style="font-weight:normal;" class="name">{$row['branch_name']}</label></td>
								<td><label class="label label-info address">{$row['address']}</label></td>
							</tr>
						{/loop}
					{else}
						<tr>
							<td align="center" colspan="3">没有有效的门店。你可以 <a href="{php echo url('activity/store/post');}" target="_blank">添加门店</a> 或 <a href="{php echo url('activity/store/import');}" target="_blank">更新门店</a></td>
						</tr>
					{/if}
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary js-select-store" data-dismiss="modal">确定</button>
			</div>
		</div>
	</div>
</div>
{if COUPON_TYPE == WECHAT_COUPON}
<!--选择可用用户组弹出窗-->
<div class="modal fade" id="groups-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">选择可用会员组</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">
				<table class="table tb" style="min-width:568px;">
					<thead>
					<tr>
						<th style="width: 50px;" class="text-center">选择</th>
						<th>用户组</th>
					</tr>
					</thead>
					<tbody>
						<tr>
							<td class="text-center"><input type="checkbox" id="chk_group_0" name="groupids[]" value="0" data-name="默认分组"></td>
							<td><label for="chk_group_0" style="font-weight:normal;" class="name">默认分组</label></td>
						</tr>
					{if !empty($groups)}
						{loop $groups $group}
							<tr>
								<td class="text-center"><input type="checkbox" id="chk_group_{$group['id']}" name="groupids[]" data-name="{$group['name']}" value="{$group['id']}"></td>
								<td><label for="chk_group_{$group['id']}" style="font-weight:normal;" class="name">{$group['name']}</label></td>
							</tr>
						{/loop}
					{/if}
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary js-select-groups" data-dismiss="modal">确定</button>
			</div>
		</div>
	</div>
</div>
{/if}
<!--选择可用模块弹出窗-->
<div class="modal fade" id="modules-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">选择可用模块</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">
				<table class="table tb" style="min-width:568px;">
					<thead>
					<tr>
						<th style="width: 35px;" class="text-center">选择</th>
						<th style="width: 100px;">模块名称</th>
						<th style="width: 100px;">标识</th>
					</tr>
					</thead>
					<tbody class="module-list">
					{loop $module $row}
					{if !in_array($row['type'], array('system'))}
					<tr>
						<td class="text-center"><input type="checkbox" id="chk_module_{$row['name']}" name="modules[]" data-name="{$row['title']}" value="{$row['name']}"></td>
						<td><label for="chk_module_{$row['name']}" style="font-weight:normal;" class="title">{$row['title']}</label></td>
						<td><label class="label label-info">{$row['name']}</label></td>
					</tr>
					{/if}
					{/loop}
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary js-select-modules" data-dismiss="modal">确定</button>
			</div>
		</div>
	</div>
</div>
<script>
	$(document).on('click', '.js-location-store-modal', function() {
		$('#location-store-modal').modal('show');
	});
	$(document).on('click', '.js-modules-modal', function() {
		$('#modules-modal').modal('show');
	});
	$(document).on('click', '.js-groups-modal', function() {
		$('#groups-modal').modal('show');
	});
	$('.list li,.card-dispose,.shop-panel').hover(function(){
		$(this).find('.cicon').show();
		$(this).addClass('hover');
	}, function(){
		$(this).find('.cicon').hide();
		$(this).removeClass('hover');
	});
	$('#form1').mouseover(function(){
		var bg_color = $(':input[name="color-value"]').val();
		if(!bg_color) {
			bg_color = '#a9d92d';
		}
		$('#color-purview').css('background-color', bg_color);
		if($(':input[name="title"]').val()) {
			$('#title-purview').html($(':input[name="title"]').val());
		}
		if($(':input[name="sub_title"]').val()) {
			$('#sub-title-purview').html($(':input[name="sub_title"]').val());
		}
		if($(':input[name="brand_name"]').val()) {
			$('#brand_name').html($(':input[name="brand_name"]').val());
		}
		$('.logo-area img').attr('src', $('#logo_upload img').attr('src'));
		var time_type = $(':radio[name="time_type"]:checked').val();
		var time = '';
		if(time_type == 1) {
			var startime = $("input[name='time_limit[start]']").val();
			var endtime = $("input[name='time_limit[end]']").val();
			time = '有效期:'+startime+'~'+endtime;
		} else if(time_type == 2) {
			var deadline = parseInt($('#deadline').val());
			var limit = parseInt($('#limit').val());
			var now = new Date();
			now.setHours(0, 0, 0);
			var unixtime =Date.parse(now)/1000;
			unixtime += (deadline*86400);
			var startime = new Date(parseInt(unixtime) * 1000).toLocaleString().substr(0,9).replace(/\//g, "-");
			unixtime += (limit*86400);
			var endtime = new Date(parseInt(unixtime) * 1000).toLocaleString().substr(0,9).replace(/\//g, "-");
			time = '有效期:'+startime+'~'+endtime;
		}
		$('#sub-time-purview').html(time);
	});

	$('.area').click(function() {
		$(this).find('.cicon a').children().trigger('click');
	});
	$('.cicon a').click(function(){
		$('.form').addClass('hidden');
		var top = $(this).offset().top;
		$('#'+$(this).attr('data-id')).css('margin-top',(top-300))
		$('#'+$(this).attr('data-id')).removeClass('hidden');
		return false;
	})

	$('#form2').mouseover(function(){
		var code_type = $('#form2 :radio[name="code_type"]:checked').val() || 0;
		if(code_type) {
			$('#destroy_title').hide();
			$('.code_preview').hide();
			$('.code_preview_' + code_type).show();
		}
		$('#notice-purview').html($('#form2 :text[name="notice"]').val());
	});

	$('#coupon-form').submit(function(){
		if(!$.trim($(':input[name="brand_name"]').val())) {
			util.message('请填写商户名称', '','error');
			return false;
		}

		if(!$.trim($(':input[name="color-value"]').val())) {
			util.message('请选择卡券颜色', '','error');
			return false;
		}

		{if $type == COUPON_TYPE_DISCOUNT}
		if(!$.trim($(':text[name="discount"]').val())) {
			util.message("折扣额度不合法",'','error');
			return false;
		}
		{elseif $type == COUPON_TYPE_CASH}
		if(!$.trim($(':text[name="reduce_cost"]').val())) {
			util.message("减免金额不合法",'','error');
			return false;
		}
		if(!$.trim($(':text[name="least_cost"]').val())) {
			util.message("抵扣金额不合法",'','error');
			return false;
		}
		{/if}
		if(!$.trim($(':input[name="title"]').val())) {
			util.message('{$coupon_title}标题不能为空', '','error');
			return false;
		}

		var time_type = $(':radio[name="time_type"]:checked').val();
		if(!time_type) {
			util.message('请完善{$coupon_title}有效期', '','error');
			return false;
		}

		var quantity = parseInt($.trim($(':text[name="quantity"]').val()));
		if(isNaN(quantity)) {
			util.message("库存只能是大于0的数字",'','error');
			return false;
		}
		{if COUPON_TYPE == WECHAT_COUPON}
		var code_type = $(':radio[name="code_type"]:checked').val();
		if(!code_type) {
			util.message('请选择销券方式','','error');
			return false;
		}
		if(!$.trim($(':text[name="notice"]').val())) {
			util.message('请完善操作提示', '','error');
			return false;
		}
		{/if}
		if(!$.trim($('textarea[name="description"]').val())) {
			util.message('请完善使用须知', '','error');
			return false;
		}
		
		$('#submit').attr('disabled', true);
		return true;
	});

	//选择门店
	$('.js-select-store').click(function(){
		var chks = $('input[name="locationids[]"]:checked');
		var locations = [];
		$('.js-location-contain').html('')
		if(chks.length>0){
			$.each(chks, function(){
				$('.js-location-contain').append('<span class="label label-success">'+$(this).data('name')+'</span>&nbsp;');
				locations.push($(this).val());
			});
			$('input[name=location-select]').val(locations.join('-'));
		} else {
			$('input[name="location-select"]').val('');
		}
	});
	//选择可用用户组
	$('.js-select-groups').click(function(){
		var chks = $('input[name="groupids[]"]:checked');
		var groupids = [];
		$('.js-groups-contain').html('');
		if (chks) {
			$.each(chks, function(){
				$('.js-groups-contain').append('<span class="label label-success">'+$(this).data('name')+'</span>&nbsp;');
				groupids.push($(this).val());
			});
			$('input[name=groups]').val(groupids.join('-'));
		} else {
			$('input[name=groups]').val('');
		}
	});
	//选择模块
	$('.js-select-modules').click(function(){
		var chks = $('input[name="modules[]"]:checked');
		var moduleids = [];
		$('.js-modules-contain').html('');
		if (chks) {
			$.each(chks, function(){
				$('.js-modules-contain').append('<span class="label label-success pull-left" style="margin:5px ;">'+$(this).data('name')+'</span>&nbsp;');
				moduleids.push($(this).val());
			});
			$('input[name=module-select]').val(moduleids.join('@'));
		} else {
			$('input[name=module-select]').val('');
		}
	});
	//初始化checkbox状态
	$('input[name$="[]"]').prop('checked', false);
</script>
{template 'common/footer'}